
import { Injectable } from '@angular/core';
import { HttpHeaders, HttpClient, HttpParams } from '@angular/common/http';
import { Observable, of } from 'rxjs';

import { environment } from '../environments/environment';
import { Asset } from '../models/Asset';
import { AssetType } from '../models/AssetType';

@Injectable()
export class AssetService {

  private baseUrl =  environment.apiUrl + 'api/assetinfo';
  private assetTypeUrl = environment.apiUrl + 'api/assettype';

  constructor(
        private http: HttpClient
  ) {}

  getAssets(current: number = 1, pageSize: number = environment.pageSize, options: {assetName?: string, assetUserId?: string, assetDepartmentId?: string, assetType?: string, assetStatus?: string, orders?: string[], outQRCode?: boolean }): Observable<{res: Asset[], success: boolean, total: number}> {
    const paramsObj: any = {
      pageSize: String(pageSize),
      page: String(current),
    };
    if (options.assetName) paramsObj.assetName = options.assetName;
    if (options.assetUserId) paramsObj.assetUserId = options.assetUserId;
    if (options.assetDepartmentId) paramsObj.assetDepartmentId = options.assetDepartmentId;
    if (options.assetType) paramsObj.assetType = options.assetType;
    if (options.assetStatus) paramsObj.assetStatus = Number(options.assetStatus);
    if (options.orders) paramsObj.orders = options.orders;
    if (options.outQRCode) paramsObj.outQRCode = options.outQRCode;
    const params = new HttpParams({ fromObject: paramsObj });
    return this.http.get<{res: Asset[], success: boolean, total: number}>(`${this.baseUrl}/getlist`, { params : params});
  }

    getAssetTypes(): Observable<{res: AssetType[], success: boolean}> {
      return this.http.get<{res: AssetType[], success: boolean}>(`${this.assetTypeUrl}/getlisttree`);
    }
  // getAssetTypes(): Observable<{res: AssetType[], success: boolean}> {
  //   return of({res: [
  //     {
  //       gdzclbmc: '办公设备',
  //       children: [
  //         {sjgdzclb_DM: '001', gdzclbmc: '鼠标'},
  //         {sjgdzclb_DM: '002', gdzclbmc: '键盘'}]
  //     },
  //   {
  //     gdzclbmc: '家具',
  //     children: [
  //       {sjgdzclb_DM: '001', gdzclbmc: '柜子'},
  //       {sjgdzclb_DM: '002', gdzclbmc: '床'}
  //     ]
  //   }
  //   ], success: true});
  // }

    getAsset(id: string): Observable<{res: Asset, success: boolean}> {
        return this.http.get<{res: Asset, success: boolean}>(`${this.baseUrl}/getbyid/${id}`);
    }

    update(asset: Asset): Observable<{res: Asset, success: boolean}> {
        const url = `${this.baseUrl}/update`;
        return this.http
            .post<{res: Asset, success: boolean}>(url, asset);
    }

    create(asset: Asset): Observable<{success: boolean, res: Asset}> {
        return this.http
            .post<{success: boolean, res: Asset}>(`${this.baseUrl}/insert`, asset);
    }

    del(id: string): Observable<{success: boolean}> {
        const url = `${this.baseUrl}/del/${id}`;
        return this.http.post<{success: boolean}>(url, {});
    }

    updateCode(id: string): Observable<{success: boolean}> {
    return this.http.get<{success: boolean}>(`${this.baseUrl}/updateqr?id=${id}`);
    }

}
